﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Soundelle.Oscillator
{
    public class GeneratorSine : BaseDevice
    {
        public GeneratorSine()
        {
        }

        #region "Parameters"

        private ParameterType[] _AvailableParameters = { 
            ParameterType.Parameter_DC_Offset,
            ParameterType.Parameter_Amplitude,
            ParameterType.Parameter_Frequency,
            ParameterType.Parameter_Phase
        };
        override public ParameterType[] AvailableParameters
        {
            get
            { return _AvailableParameters; }
        }
       

        #endregion

        private DeviceFunctionType[] _AvailableFunctions = new DeviceFunctionType[] {
             DeviceFunctionType.Function_GeneratorSine
        };
        override public DeviceFunctionType[] AvailableFunctions
        {
            get
            { return _AvailableFunctions; }
        }

        override protected void InitRmsRatio()
        {
            RmsRatio = Math.Sqrt(2.0) / 2.0;
        }

        override public double SignalOut()
        {
            if (!Enabled)
                return 0.0;

            double signalOut = 0.0;
            double signalIn = 0.0;

            double DcOffset = Parameter_DC_Offset;
            double amplitude = Parameter_Amplitude;
            double frequency = 0.0;
            double phase = Parameter_Phase;

            double duty = 0.0;

            DoModulationByChildDevices(ref signalOut, ref signalIn, ref amplitude, ref frequency, ref phase, ref duty);
            frequency = Parameter_Frequency;

            double sig = amplitude * Math.Sin(2.0 * Math.PI * frequency * TimePosition / SamplingRate + phase);

            ++TimePosition;
            double SignalOut = sig + amplitude * signalIn + signalOut + DcOffset;
            SetPreviousSignalOut(SignalOut);
            return SignalOut;
        }
    }
}
